package com.imm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imm.common.Result;
import com.imm.mapper.ShoppingCartMapper;
import com.imm.pojo.ShoppingCart;
import com.imm.service.IShoppingCartService;
import com.imm.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements IShoppingCartService {
    @Override
    public Result<List<ShoppingCart>> listShoppingCart(ShoppingCart shoppingCart) {
        Long userId = BaseContext.get();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId)
                .eq(shoppingCart.getId() != null, ShoppingCart::getId, shoppingCart.getId());
        List<ShoppingCart> list = list(wrapper);
        return Result.success(list);
    }

    @Transactional
    @Override
    public Result<String> add(ShoppingCart shoppingCart) {
        Long userId = BaseContext.get();
        shoppingCart.setUserId(userId);
        // 先查看是否存在，存在则修改number，number = number + 1
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                .eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId())
                .eq(ShoppingCart::getUserId, userId);
        ShoppingCart one = getOne(queryWrapper);
        log.info("购物车信息：{}", one);
        // 此处要判断是否为当前用户所以创建
        if (one != null) {
            LambdaUpdateWrapper<ShoppingCart> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(ShoppingCart::getNumber, one.getNumber() + 1)
                    .eq(ShoppingCart::getId, one.getId());
            update(updateWrapper);
        } else {
            save(shoppingCart);
        }
        return Result.success("添加成功");
    }

    @Override
    public Result<String> sub(ShoppingCart shoppingCart) {
        Long userId = BaseContext.get();
        shoppingCart.setUserId(userId);
        // 先查看是否存在，存在则修改number，number = number - 1
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId())
                .eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId())
                .eq(ShoppingCart::getUserId, userId);
        ShoppingCart one = getOne(queryWrapper);
        if (one != null && one.getNumber() > 1) {
            LambdaUpdateWrapper<ShoppingCart> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.set(ShoppingCart::getNumber, one.getNumber() - 1)
                    .eq(ShoppingCart::getId, one.getId());
            update(updateWrapper);
        } else {
            remove(queryWrapper);
        }
        return Result.success("移除成功");
    }

    @Override
    public Result<String> clean() {
        Long userId = BaseContext.get();
        LambdaQueryWrapper<ShoppingCart> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, userId);
        remove(wrapper);
        return Result.success("清除成功");
    }
}
